<?php

/**
 * Base model
 *
 * @package
 */

	define("TABLE_PREFIX", "srcms_");
	define("TABLE_MENU", TABLE_PREFIX . "menu");
	define("TABLE_MODULES", TABLE_PREFIX . "modules");
	define("TABLE_USERS", TABLE_PREFIX . "users");
	define("TABLE_OPTIONS", TABLE_PREFIX . "options");
	define("TABLE_LANGUAGES", TABLE_PREFIX . "languages");
	define("TABLE_FILES", TABLE_PREFIX . "files");
	define("TABLE_SEARCH", TABLE_PREFIX . "search");
	define("TABLE_BOX_IN_MENU", TABLE_PREFIX . "box_in_menu");

abstract class BaseModel extends Object
{
	/**#@+ Database table constants */
	
	/**#@-*/
	
	public $session;
	public $session_web;
	public $translation;
	/**
	 * Prevent users to clone the instance
	 */
	final public function __clone()
	{
		throw new Exception('Clone is not allowed');
	}

	/**
	 * Prevent users to unserialize the instance
	 */
	final public function __wakeup()
	{
		throw new Exception('Unserialization is not allowed');
	}

	/**
	 * Instance initialization
	 * @param string $table
	 */
	protected function _init()
	{
		$this->session = Environment::getSession()->getNamespace("system");
		$this->session_web = Environment::getSession()->getNamespace("web");
	}
	
	public function createSeo($name){
		$address = $name;
		 
		// nahradi znaky s diakritikou na znaky bez diakritiky
		$fromTo = array("Á" => "A", "Ä" => "A", "Č" => "C", "Ç" => "C", "Ď" => "D", "É" => "E", "Ě" => "E", "Ë" => "E", "Í" => "I", "Ň" => "N", "Ó" => "O", "Ö" => "O", "Ř" => "R", "Š" => "S", "Ť" => "T", "Ú" => "U", "Ů" => "U", "Ü" => "U", "Ý" => "Y", "Ž" => "Z", "á" => "a", "ä" => "a", "č" => "c", "ç" => "c", "ď" => "d", "é" => "e", "ě" => "e", "ë" => "e", "í" => "i", "ň" => "n", "ó" => "o", "ö" => "o", "ř" => "r", "š" => "s", "ť" => "t", "ú" => "u", "ů" => "u", "ü" => "u", "ý" => "y", "ž" => "z", " " => "-");
		$address = StrTr ($address, $fromTo);
		 
		// prevede vsechna velka pismena na mala
		$address = strtolower ($address);
		 
		// nahradi pomlckou vsechny znanky, ktera nejsou pismena
		$re = "/[^[:alpha:][:digit:]]/";
		$replacement = "-";
		$address = preg_replace ($re, $replacement, $address);
		 
		// odstrani ze zacatku a z konce retezce pomlcky
		$address = trim ($address, "-");
		 
		// odstrani z adresy pomlcky, pokud jsou dve a vice vedle sebe
		$re = "/[-]+/";
		$replacement = "-";
		$address = preg_replace ($re, $replacement, $address);

		return $address;
	}
	
	public function createSeoFile($name){
			$address = $name;
			 
			// nahradi znaky s diakritikou na znaky bez diakritiky
			$fromTo = array("Á" => "A", "Ä" => "A", "Č" => "C", "Ç" => "C", "Ď" => "D", "É" => "E", "Ě" => "E", "Ë" => "E", "Í" => "I", "Ň" => "N", "Ó" => "O", "Ö" => "O", "Ř" => "R", "Š" => "S", "Ť" => "T", "Ú" => "U", "Ů" => "U", "Ü" => "U", "Ý" => "Y", "Ž" => "Z", "á" => "a", "ä" => "a", "č" => "c", "ç" => "c", "ď" => "d", "é" => "e", "ě" => "e", "ë" => "e", "í" => "i", "ň" => "n", "ó" => "o", "ö" => "o", "ř" => "r", "š" => "s", "ť" => "t", "ú" => "u", "ů" => "u", "ü" => "u", "ý" => "y", "ž" => "z", " " => "-");
			$address = StrTr ($address, $fromTo);
			 
			// prevede vsechna velka pismena na mala
			$address = strtolower ($address);
			 
			// odstrani ze zacatku a z konce retezce pomlcky
			$address = trim ($address, "-");
			 
			// odstrani z adresy pomlcky, pokud jsou dve a vice vedle sebe
			$re = "/[-]+/";
			$replacement = "-";
			$address = preg_replace ($re, $replacement, $address);
	
			return $address;
	}
	
	public function removeDir($dir) {
	   if (is_dir($dir)) {
	     $objects = scandir($dir);
	     foreach ($objects as $object) {
	       if ($object != "." && $object != "..") {
	         if (filetype($dir."/".$object) == "dir") self::removeDir($dir."/".$object); else unlink($dir."/".$object);
	       }
	     }
	     reset($objects);
	     rmdir($dir);
	   }
 	} 
 	
	function formatSize($size) {
	      $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
	      if ($size == 0) { return('n/a'); } else {
	      return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); }
	}
	
	function formatType($type){
		
		$type = str_replace(".", "", $type);
		// nazvy pro uzivatele
		$type_name = $type;
		
		$type = "images/ikonky/".strtoupper($type);
		
		$type .= "-5.png";
		
		return $type;
	}
 	

}
